package com.hss01248.dialog.adapter;

import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/9.
 */

public abstract class SuperPagerAdapter extends PageSliderProvider implements Refreshable,ILifeCycle{

    List datas = new ArrayList();
    List<SuperPagerHolder> mPagerHolders = new ArrayList<SuperPagerHolder>();
    Context context;
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "SuperPagerAdapter");

    public SuperPagerAdapter(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer container, int position) {
        SuperPagerHolder holder = null;
        if (container == null)
        {
            return null;
        }
        if(position >= mPagerHolders.size()){
            holder = generateNewHolder(context,container,position);
            mPagerHolders.add(holder);
        }else {
            holder = mPagerHolders.get(position);
        }
        if(holder == null){
            holder = generateNewHolder(context,container,position);
            mPagerHolders.add(holder);
        }
        holder.assingDatasAndEvents(context,datas.get(position),position);
        container.addComponent(holder.rootView);
        return holder.rootView;

    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object obj) {
        if (componentContainer == null) {
            return;
        }
        if (obj instanceof Component) {
            componentContainer.removeComponent((Component) obj);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return component == o;
    }
    protected abstract SuperPagerHolder generateNewHolder(Context context, ComponentContainer container, int position);


    @Override
    public void refresh(List newData) {
        datas.clear();
        datas.addAll(newData);
        notifyDataChanged();
    }

    @Override
    public void addAll(List newData) {
        datas.addAll(newData);
        notifyDataChanged();
    }

    @Override
    public void clear() {
        datas.clear();
        notifyDataChanged();
    }

    @Override
    public void delete(int position) {
        datas.remove(position);
        notifyDataChanged();
    }

    @Override
    public void add(Object object) {
        datas.add(object);
        notifyDataChanged();
    }

    @Override
    public void onDestory() {

    }
}
